<html>
<head><meta charset="utf-8"><title>Explore hiding cfg excluded code · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html">Explore hiding cfg excluded code</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="201142160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201142160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201142160">(Jun 17 2020 at 13:42)</a>:</h4>
<p>Hi <span class="user-mention" data-user-id="133169">@matklad</span> , I was thinking about looking into dimming code that is not compiled with cfg, so, e.g. if you have some code that's</p>
<div class="codehilite"><pre><span></span><code>#[cfg(not(test))]
struct A { ... }

#[cfg(test)]
struct A { ... }
</code></pre></div>


<p>then one of the two could be hidden with styling. At least for vs code, that's not currently possible outside of <code>Diagnostic::Unused</code>, which doesn't seem like an appropriate usage of that tag. Do you think it's still worth that effort or add in the initial support as a possible motivator to get some support in vs code for some additional dimming outside of the diagnostic tag?</p>



<a name="201142573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201142573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201142573">(Jun 17 2020 at 13:45)</a>:</h4>
<p>Implementation wise, I think I could build off the work the work that was done with doctests in terms of replacing the highlighting stacks.</p>



<a name="201143651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201143651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201143651">(Jun 17 2020 at 13:53)</a>:</h4>
<p>I prefer to think not in terms of VS Code, but in terms of some hypothetical "perfect" IDE client. It's better if clients catch up with the server, than vice verse :)</p>
<p>For cfg, I think we need to do two things:</p>
<ul>
<li>stop appling <code>UnresovledReference</code> Tag, and fall-back on the syntax-based tag (generlizing doctest infra for this)</li>
<li>applying <code>| Disabled</code> modification</li>
</ul>



<a name="201143752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201143752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201143752">(Jun 17 2020 at 13:54)</a>:</h4>
<p>The harder bit is figuring out <em>which</em> code is disabled, and what does it mean at all... </p>
<p>Like, you can have a single crate both with and without <code>--test</code> enabled. SHould this disable <code>#[cfg(test)]</code> code?</p>



<a name="201144557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201144557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201144557">(Jun 17 2020 at 14:00)</a>:</h4>
<p>In my ideal world, it would disable <code>#[cfg(test)]</code> code when the cursor is in an item that does not have <code>#[cfg(test)]</code> on it</p>



<a name="201145600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201145600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201145600">(Jun 17 2020 at 14:07)</a>:</h4>
<p>This actually relates to a conversation I was having with <span class="user-mention" data-user-id="301314">@woody77</span> . We were thinking of a way to toggle cfg items on and off through some kind of command rather than changing a settings config. I've run into this myself in some of my own projects where some feature flags conflict with each other, and <code>--all-features</code> isn't a possible solution.</p>



<a name="201145929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201145929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201145929">(Jun 17 2020 at 14:10)</a>:</h4>
<p>The ideal UI woudl be <a href="https://github.com/intellij-rust/intellij-rust/pull/5189">https://github.com/intellij-rust/intellij-rust/pull/5189</a></p>



<a name="201146039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201146039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201146039">(Jun 17 2020 at 14:11)</a>:</h4>
<p>Oh wow I was thinking of checkboxes too! but I didn't think to place it in Cargo.toml</p>



<a name="201146065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201146065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201146065">(Jun 17 2020 at 14:11)</a>:</h4>
<p>One area where this could get confusing is if you're navigating multiple crates</p>



<a name="201146123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201146123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201146123">(Jun 17 2020 at 14:11)</a>:</h4>
<p>For example, in one of my crates I have subfolders with test crates that depend on the main crate with different features toggled on and off.</p>



<a name="201146301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201146301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201146301">(Jun 17 2020 at 14:12)</a>:</h4>
<p>Navigating through the code there, my preference would be that the context of the flags in the crates  that I'm jumping to would depend on where I jumped from. But from an internals perspective, that sounds like something that's very hard to get right.</p>



<a name="201146524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201146524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201146524">(Jun 17 2020 at 14:14)</a>:</h4>
<p>Unless maybe there's a way to say, "show all code as if this crate was the main crate". I imagine multi-crate scenarios like this are somewhat rare</p>



<a name="201146664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201146664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201146664">(Jun 17 2020 at 14:15)</a>:</h4>
<blockquote>
<p>But from an internals perspective, that sounds like something that's very hard to get right.</p>
</blockquote>
<p>That's actually how it is implemnted internally already</p>



<a name="201146720"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201146720" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201146720">(Jun 17 2020 at 14:15)</a>:</h4>
<p>The impl parts are not that difficult, you just have to allow for several instances of a crate in the crate graph</p>



<a name="201146837"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201146837" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201146837">(Jun 17 2020 at 14:16)</a>:</h4>
<p>Oh I had no clue that was in place already.</p>



<a name="201146848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201146848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201146848">(Jun 17 2020 at 14:16)</a>:</h4>
<p>The hard thing is UI: reconsiling the fact that the user sees "a struct" with a fact that this struct corresponds to 2-3 <code>hir::Struct</code> in different crates internally</p>



<a name="201147074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201147074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201147074">(Jun 17 2020 at 14:17)</a>:</h4>
<p>Which part of the code should I read to get a better understanding of how this works?</p>



<a name="201147220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201147220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201147220">(Jun 17 2020 at 14:18)</a>:</h4>
<p>I also realized, regarding the checkbox UI you linked from intellij, we'd need an alternate implementation for supporting json projects.</p>



<a name="201147349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201147349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201147349">(Jun 17 2020 at 14:19)</a>:</h4>
<p>So, the reason <em>why</em> it works is that we don't restrict CrateGraph to only have a single Crate for each root modules</p>



<a name="201147459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201147459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201147459">(Jun 17 2020 at 14:20)</a>:</h4>
<p>And if you are already in the context of a specific Crate, then you get correct dependnecies, etc</p>



<a name="201147515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201147515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201147515">(Jun 17 2020 at 14:20)</a>:</h4>
<p>The only trick happens when you translate a raw position (file <a href="http://foo.rs">foo.rs</a>, line 92, offset 62) into a crate with this file</p>



<a name="201147619"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201147619" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201147619">(Jun 17 2020 at 14:21)</a>:</h4>
<p>Here, at the moment we basically "guess": pick the first crate which matches</p>



<a name="201147621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201147621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201147621">(Jun 17 2020 at 14:21)</a>:</h4>
<p><a href="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_hir/src/semantics/source_to_def.rs#L30-L39">https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_hir/src/semantics/source_to_def.rs#L30-L39</a></p>



<a name="201917592"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/201917592" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> woody77 <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#201917592">(Jun 24 2020 at 23:47)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133169">matklad</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Explore.20hiding.20cfg.20excluded.20code/near/201146720">said</a>:</p>
<blockquote>
<p>The impl parts are not that difficult, you just have to allow for several instances of a crate in the crate graph</p>
</blockquote>
<p>Catching up _very_ belatedly...  This has been on my mind a bunch, recently, as for us, our (current) rust-project.json is a dependency graph of crates to compile, and crates can be listed multiple times, with different sets of enabled features.  I've been trying to decide if we should compress that down or not into something that looks more like Cargo.toml, where each crate is listed as a set of potential features, or the summation of all enabled features in all uses (but then what to do with conflicts).</p>
<p>I really like the ability we currently seem like we have, where we can put the stdlib into the crate graph multiple times, for different architectures (since we're building both host and embedded-target (aka cross-compilation) in the same tree), and that the graph can know which particular target arch/os we're compiling for.</p>
<p>But the UI side is hard.  In the graph, it seems easy to know that for a struct that's only in the graph once, that all of its symbols are quite clear.  But once you jump from that struct, to one of the types that it uses, which is in a file that matches multiple places in the graph...  now what?</p>
<p>Hence <span class="user-mention" data-user-id="116114">@Paul Faria</span> and I talking about how we'd go about marking which config sets to enable (although with a compiled-target approach, RA is able to know how many places in the graph that something exists, and but I'm not sure if we can send back multiple options based on features (vs. files that might match).</p>



<a name="220731521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/220731521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Barsky <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#220731521">(Dec 22 2020 at 20:36)</a>:</h4>
<p>(sorry for reviving a long-dormant discussion.)</p>
<p>I was speaking to <span class="user-mention" data-user-id="224941">@Carl Lerche</span> about vending Loom (<a href="https://github.com/tokio-rs/loom">https://github.com/tokio-rs/loom</a>) through a loom-rt crate. One of the biggest gotchas with Loom is the work needed for consumers to set up the dev-dependecy'd Loom that (optionally) replaces std's sync and thread imports based on feature flags. it's not the best experience. What I wanted to ask: I know the checkboxes might be optimal approach for some feature flags, but given predicable <em>scope</em> of swapped imports, can rust-analyzer try to import loom-vended types in <code>#[cfg(loom)]</code> target and std's types in <code>#[cfg(not(loom)]</code>?</p>



<a name="220731553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Explore%20hiding%20cfg%20excluded%20code/near/220731553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> David Barsky <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Explore.20hiding.20cfg.20excluded.20code.html#220731553">(Dec 22 2020 at 20:36)</a>:</h4>
<p>(feel free to reparent my comment)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>